TK!2
=v
#1
:n
n
:v
10
:s
i
#2
:n
k
:v
5
:s
i
#4
:n
f1
:v
3628800
:s
o
#5
:n
f2
:v
3628800
:s
o
#6
:n
f3
:v
3628799.999999719
:s
o
#7
:n
f4
:v
3628800
:s
o
#9
:n
nok1
:v
252
:s
o
#10
:n
nok2
:v
252.0000000000195
:s
o
#11
:n
nok3
:v
252
:s
o
#12
:n
nok4
:v
252
:s
o
=u
=l
=f
#1
:n
Factorialr
:c
Factorial, recursive definition
:t
Procedure
:i
n
:o
z
#2
:n
Factoriali
:c
Factorial, iterative definition
:t
Procedure
:i
n
:o
z
#3
:n
NOKr
:c
Binomial coefficient, recursive definition
:t
Procedure
:i
n,k
:o
z
#4
:n
NOKi
:c
Binomial coefficient, iterative definition
:t
Procedure
:i
n,k
:o
z
#1
:n
Factorialr
:z
if n>1 then z:= n*Factorialr(n-1) else z:= 1

" Definition intervals:
"   0 ó n ó 21  . . .  procedure returns a correct value
"  22 ó n ó 25  . . .  procedure returns an approximate value; rounding-off
"                      error due to 16 significant digits in TK
"       n > 25  . . .  procedure fails because of TK limit on recursive
"                      calls nesting
/E
E
#2
:n
Factoriali
:z
z:= 1
for i = 2 to n
   z:= i*z
next i

" Definition intervals: 
"   0 ó n ó 21  . . .  procedure returns a correct value
"  26 ó n ó 170 . . .  returns an approximate value; rounding-off error
"                      due to 16 significant digits in TK
"       n > 170 . . .  procedure fails because of TK limit on floating
"                      point numbers
/E
E
#3
:n
NOKr
:z
if k<0 then error:= k_cannot_be_negative
if k>0 then z:= n/k*NOKr(n-1,k-1)  else z:= 1

" Note: procedure fails when  k > 25  because TK limit on recursive
"       calls nesting; use the TK library function NOKi.
/E
E
#4
:n
NOKi
:z
if k<0 then error:= k_cannot_be_negative
n1:= n+1
z:= 1
for i=1 to k
   z:= z/i*(n1-i)
next i
/E
E
=r
#1
:r
"  ***  Factorials and binomial coefficients  ***
:s
C
#3
:r
" F a c t o r i a l :
:s
C
#4
:r
f1 = Factorialr(n)  " recursive function, see the Function Subsheet for notes
#5
:r
f2 = Factoriali(n)  " iterative function, see the Function Subsheet for notes
#6
:r
f3 = gamma(n+1)           " advantage: gamma function is invertible
#7
:r
                          " disadvantage: relative error ÷ 1e-14
:s
C
#8
:r
f4 = round(gamma(n+1))    " correct for  0 ó n ó 16
#9
:r
                          " for  n>16  the same as gamma(n+1)
:s
C
#11
:r
" B i n o m i a l   c o e f f i c i e n t :
:s
C
#12
:r
nok1 = Factorialr(n)/Factorialr(n-k)/Factorialr(k)  " use restricted by n<26
#13
:r
nok2 = gamma(n+1)/gamma(n-k+1)/gamma(k+1)           "use restricted by n<170
#14
:r
nok3 = NOKr(n,k)    " recursive function, use restricted by k<26
#15
:r
nok4 = NOKi(n,k)    " iterative function, use restricted by nok2<1e308
#17
:r
" Note: This model offers several ways and library functions for evaluating
:s
C
#18
:r
"       factorials and binomial coefficients. Any particular one should be
:s
C
#19
:r
"       chosen depending the required definition interval and precision
:s
C
#20
:r
"       requirements. Extra large arguments may be handled by ALI functions.
:s
C
=p
=t
=n
=g
#1
:c
.000001
:i
10
:v
Yes
:l
60
:f
66
:m
0
:w
80
:b
Yes
:a
Yes
:r
Yes
:h
No
:t
1
:n
Yes
:d
LPT1
%Tr,f,0,1,0,1,0
